package com.aispeech.astools.view;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.aispeech.astools.R;
import com.aispeech.astools.bean.SysTestItem;
import com.aispeech.astools.service.FloatingService;
import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

public class SysIntTestActivity extends AppCompatActivity implements OnItemClickListener {

    private final static String TAG = "SysIntTestActivity";

    private AudioManager mAudioManager;
    private ListView mListView;
    private LvAdapter mLvAdapter;
    private ArrayList<SysTestItem> mListItems;
    private int mMuteFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_int_test);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        initView();
        mListView.setItemsCanFocus(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    private void initView() {
        mListView = findViewById(R.id.lv_sys_test);
        mListItems = new ArrayList<>();

        SysTestItem audioTestItem = new SysTestItem();
        audioTestItem.setName("媒体音量");
        audioTestItem.setImgSrc(R.drawable.audio_24);
        audioTestItem.setType(SysTestItem.TYPE_VOLUME_MUSIC_TEST);
        mListItems.add(audioTestItem);

        SysTestItem alarmTestItem = new SysTestItem();
        alarmTestItem.setName("闹铃音量");
        alarmTestItem.setImgSrc(R.drawable.alarm_24);
        alarmTestItem.setType(SysTestItem.TYPE_VOLUME_ALARM_TEST);
        mListItems.add(alarmTestItem);

        SysTestItem floatWindowTestItem = new SysTestItem();
        floatWindowTestItem.setName("悬浮窗测试");
        floatWindowTestItem.setImgSrc(R.drawable.fwindows_24);
        floatWindowTestItem.setType(SysTestItem.TYPE_FLOAT_WINDOW_TEST);
        mListItems.add(floatWindowTestItem);

        mLvAdapter = new LvAdapter();
        mListView.setAdapter(mLvAdapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {

            case 2:
            startFloatingService();
            break;
        }
    }

    public void startFloatingService() {
        startService(new Intent(SysIntTestActivity.this, FloatingService.class));
    }


    private class LvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mListItems == null ? 0 : mListItems.size();
        }

        @Override
        public Object getItem(int i) {
            if (null != mListItems) {
                return mListItems.get(i);
            }
            return null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            SysTestItem sysTestItem = mListItems.get(i);

            if (convertView == null) {
                convertView = SysIntTestActivity.this.getLayoutInflater().inflate(R.layout.list_test_item, null);
                viewHolder = new SysIntTestActivity.ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (SysIntTestActivity.ViewHolder) convertView.getTag();
            }

            viewHolder.textView.setText(sysTestItem.getName());
            viewHolder.imageView.setBackgroundResource(sysTestItem.getImgSrc());

            switch (sysTestItem.getType()) {
                case SysTestItem.TYPE_VOLUME_MUSIC_TEST:
                    viewHolder.imageView.setOnClickListener(new VolumeMuteClickListener(AudioManager.STREAM_MUSIC));

                    int musicStreamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                    Log.d(TAG, "getView: MusicStreamMaxVolume = " + musicStreamMaxVolume);
                    viewHolder.seekBar.setMax(musicStreamMaxVolume);
                    viewHolder.seekBar.setVisibility(View.VISIBLE);
                    viewHolder.seekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
                    viewHolder.seekBar.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(AudioManager.STREAM_MUSIC));
                    break;
                case SysTestItem.TYPE_VOLUME_ALARM_TEST:
                    viewHolder.imageView.setOnClickListener(new VolumeMuteClickListener(AudioManager.STREAM_ALARM));

                    int alarmStreamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
                    Log.d(TAG, "getView: AlarmStreamMaxVolume = " + alarmStreamMaxVolume);
                    viewHolder.seekBar.setMax(alarmStreamMaxVolume);
                    viewHolder.seekBar.setVisibility(View.VISIBLE);
                    viewHolder.seekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM));
                    viewHolder.seekBar.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(AudioManager.STREAM_ALARM));
                    break;
                case SysTestItem.TYPE_FLOAT_WINDOW_TEST:
                    viewHolder.seekBar.setVisibility(View.GONE);
                    break;
            }

            return convertView;
        }
    }

    private class VolumeMuteClickListener implements View.OnClickListener{
        private int mType;

        public VolumeMuteClickListener(int type) {
            this.mType = type;
        }

        @Override
        public void onClick(View v) {
            switch (mType) {
                case AudioManager.STREAM_MUSIC:
                    if ((mMuteFlag & 1) == 1) {

                        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
//                view.setBackgroundColor(Color.WHITE);
                        mMuteFlag &= 2;
                        Toast.makeText(SysIntTestActivity.this, "unmute music stream : " + mMuteFlag, Toast.LENGTH_SHORT).show();
                    } else {
                        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
                        int musicStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                        if (musicStreamVolume == 0) {
//                    view.setBackgroundColor(Color.RED);
                            mMuteFlag |= 1;
                            Toast.makeText(SysIntTestActivity.this, "mute music stream : " + mMuteFlag, Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(SysIntTestActivity.this, "can not mute music stream", Toast.LENGTH_SHORT).show();
                            mMuteFlag &= 2;
                        }
                    }

                    break;
                case AudioManager.STREAM_ALARM:
                    if ((mMuteFlag >> 1) == 1) {
                        mAudioManager.setStreamMute(AudioManager.STREAM_ALARM, false);
//                view.setBackgroundColor(Color.WHITE);
                        mMuteFlag &= 1;
                        Toast.makeText(SysIntTestActivity.this, "unmute alarm stream : " + mMuteFlag, Toast.LENGTH_SHORT).show();
                    } else {
                        mAudioManager.setStreamMute(AudioManager.STREAM_ALARM, true);
                        int alarmStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
                        if (alarmStreamVolume == 0) {
//                    view.setBackgroundColor(Color.RED);
                            mMuteFlag |= 2;
                            Toast.makeText(SysIntTestActivity.this, "mute alarm stream : " + mMuteFlag, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SysIntTestActivity.this, "can not mute alarm stream", Toast.LENGTH_SHORT).show();
                            mMuteFlag &= 1;
                        }
                    }

                    break;
            }
        }


    }

    private class VolumeSeekBarChangeListener implements OnSeekBarChangeListener {

        private int mType;

        public VolumeSeekBarChangeListener(int type) {
            this.mType = type;
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Log.d(TAG, "onProgressChanged with: mType = " + mType + ", progress = " + progress + ", fromUser = " + fromUser + "");
            if (fromUser) {
                mAudioManager.setStreamVolume(mType, progress, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            } else {
                Log.d(TAG, "onProgressChanged: no need change");
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            Log.d(TAG, "onStartTrackingTouch: mType = " + mType);
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            Log.d(TAG, "onStopTrackingTouch: mType = " + mType);
        }
    }

    private final class ViewHolder {

        TextView textView;
        ImageView imageView;
        SeekBar seekBar;

        ViewHolder(View view) {
            textView = view.findViewById(R.id.tv_test);
            imageView = view.findViewById(R.id.imgv_volume_type);
            seekBar = view.findViewById(R.id.sb_volume_set);
        }
    }

}
